/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.product;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.faces.event.ValueChangeEvent;
import org.hutech09.jsale.bsn.ICustomerManager;
import org.hutech09.jsale.entity.Customer;
import org.hutech09.jsale.entity.Product;
import org.hutech09.jsale.web.customer.cusController;

import org.primefaces.component.datatable.DataTable;

/**
 *
 * @author abc
 */
@ManagedBean
@SessionScoped
public class cartController implements Serializable {

    @ManagedProperty(value = "#{cusController}")
    private cusController cusController;
    @EJB
    private ICustomerManager iCustomerManager;
    private List<String> images;
    private long sumPriceByUser;
    private int count=0;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
    
    public long getSumPriceByUser() {
              if (getCusController().islogin()) {
                 
            Customer customer = iCustomerManager.find(getCusController().getCus().getId());
            for (cartModel cmd : listcartmodel) {
                
                if (cmd.getCustomer().getId().equals(customer.getId())) {
                    sumPriceByUser = cmd.getSumprice();
                }
            }
        }
        else
        {
            sumPriceByUser =0;
        }
        return sumPriceByUser;
    }

    public void setSumPriceByUser(long sumPriceByUser) {
        this.sumPriceByUser = sumPriceByUser;
    }

    public List<String> getImages() {
        images = new ArrayList<String>();
        images.add("i1.jpg");
        images.add("i2.jpg");
        images.add("i3.jpg");
        images.add("i4.jpg");
        return images;
    }

    public cusController getCusController() {
        return cusController;
    }

    public void setCusController(cusController cusController) {
        this.cusController = cusController;
    }
    private Product pro;

    public Product getPro() {
        return pro;
    }

    public void setPro(Product pro) {
        this.pro = pro;
    }
    
    private List<Integer> dropdownlist;

    public List<Integer> getDropdownlist() {
         dropdownlist = new ArrayList<Integer>();
        for (int i = 0; i < 20; i++) {
           dropdownlist.add(i);
        }
                return dropdownlist;
    }

    public void setDropdownlist(List<Integer> dropdownlist) {
        this.dropdownlist = dropdownlist;
    }
    
    public void updateSoLuong(ValueChangeEvent e){
        
    }
       
    
    
    private cartModel card = new cartModel();

    public cartModel getCard() {
        count = 0;
      if (getCusController().islogin()) {
            Customer customer = iCustomerManager.find(getCusController().getCus().getId());
            for (cartModel cmd : listcartmodel) {
                if (cmd.getCustomer().getId().equals(customer.getId())) {
                    this.card = cmd;
                        for (productModel prmd : cmd.getListPromodel()) {
                        count=count+(int)prmd.getSoluong();
                    }
                  }
            }
        }else{
                    this.count=0;
                    card=null;
                    card=new cartModel();;
                }
    
        return card;
    }

    public void setCard(cartModel card) {
        this.card = card;
    }
    
    private List<cartModel> listcartmodel = new ArrayList<cartModel>();

    public List<cartModel> getListcartmodel() {
        return listcartmodel;
    }

    public void setListcartmodel(List<cartModel> listcartmodel) {
        this.listcartmodel = listcartmodel;
    }

    public void addtoCart(productDetail pros) {
        cartModel cartmodel = new cartModel();
        productModel promodel = new productModel();;
        FacesContext context = FacesContext.getCurrentInstance();
        if (getCusController().islogin()) {
            Customer customer = iCustomerManager.find(getCusController().getCus().getId());
            if (listcartmodel.isEmpty()) {
                cartmodel = new cartModel();
                cartmodel.setCustomer(customer);
                promodel = new productModel(pros, 1);
                cartmodel.getListPromodel().add(promodel);
                cartmodel.setSumprice(pros.getPrice());
                listcartmodel.add(cartmodel);
                this.sumPriceByUser=cartmodel.getSumprice();
                this.card = cartmodel;
                context.addMessage(null, new FacesMessage("Thêm vào giỏ hàng thành công"));
            } else {
                boolean checkuser = false;
                for (cartModel cmd : listcartmodel) {
                    ///---nếu list cart có user là user hiện đăng nhập--/////
                    if (cmd.getCustomer().getId().equals(customer.getId())) {
                        boolean added = false;
                        for (productModel proMD : cmd.getListPromodel()) {
                            //-nếu trùng sản phẩm thì tăng thêm 1-//
                            if (proMD.getProdetail().getProduct().getId().equals(pros.getProduct().getId())) {
                                proMD.setSoluong(proMD.getSoluong() + 1);
                                cmd.setSumprice(cmd.getSumprice() + proMD.getProdetail().getPrice());
                                added = true;
                                this.card = cmd;
                                this.sumPriceByUser = cmd.getSumprice();
                                context.addMessage(null, new FacesMessage("Thêm vào giỏ hàng thành công"));
                            }
                        }
                        //-nếu kết thúc vòng for mà không có trùng thì tạo mới và thêm vào-//
                        if (!added) {
                            promodel = new productModel(pros, 1);
                            cmd.getListPromodel().add(promodel);
                            cmd.setSumprice(cmd.getSumprice() + pros.getPrice());
                            this.card = cmd;
                            this.sumPriceByUser = cmd.getSumprice();
                            context.addMessage(null, new FacesMessage("Thêm vào giỏ hàng thành công"));
                        }
                        checkuser = true;
                    }
                }
                if (!checkuser) {
                    cartmodel = new cartModel();
                    cartmodel.setCustomer(customer);
                    promodel = new productModel(pros, 1);
                    cartmodel.getListPromodel().add(promodel);
                    cartmodel.setSumprice(pros.getPrice());
                    listcartmodel.add(cartmodel);
                    this.card = cartmodel;this.sumPriceByUser=cartmodel.getSumprice();
                    context.addMessage(null, new FacesMessage("Thêm vào giỏ hàng thành công"));
                }
            }
        }
    }

    public void delete(productDetail pro) {
        FacesContext context = FacesContext.getCurrentInstance();
        for (productModel model : card.getListPromodel()) {
            if (model.getProdetail().getProduct().getId().equals(pro.getProduct().getId())) {
                if (model.getSoluong() == 1) {
                    card.getListPromodel().remove(model);
                    card.setSumprice(card.getSumprice() - pro.getPrice());
                    context.addMessage(null, new FacesMessage("Xóa thành công"));
                    break;
                } else {
                    model.setSoluong(model.getSoluong() - 1);
                    card.setSumprice(card.getSumprice() - pro.getPrice());
                    context.addMessage(null, new FacesMessage("Xóa thành công"));
                    break;
                }
            }

        }
        for (cartModel cmd  : listcartmodel) {
            if(cmd.getCustomer().getId().equals(card.getCustomer().getId()))
            {
                cmd.setListPromodel(card.getListPromodel());
            }
        }
    }
    
    public String oder()
    {
        
        return "oder";
    }
    
    public void test() {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("asda Login"));
    }

    public cartController() {
    }
}
